package com.bleeth.fx.helper;

import cn.hutool.core.util.HexUtil;
import cn.hutool.core.util.RandomUtil;
import cn.hutool.core.util.StrUtil;
import com.bleeth.fx.layout.FormBuilder;
import com.bleeth.fx.layout.GirdCell;
import javafx.geometry.Pos;
import javafx.scene.control.Button;
import javafx.scene.control.CheckBox;
import javafx.scene.control.Hyperlink;
import javafx.scene.control.Label;
import javafx.scene.control.PasswordField;
import javafx.scene.control.TableView;
import javafx.scene.control.TextField;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.HBox;
import javafx.scene.layout.Region;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;

/**
 * @author ：Bleeth
 * @date ：2021-11-07
 * @description：组件创建工具
 */
public class ControlsHelper {


    public static Label createLabel(String text, String labelClass, String textClass) {
        Label label = new Label();
        label.setText(text);

        if (StrUtil.isNotBlank(labelClass)) {
            label.getStyleClass().add(labelClass);
        }
        if (StrUtil.isNotBlank(textClass)) {
            label.getStyleClass().add(textClass);
        }
        return label;
    }

    public static TextField createTextField(String text, String tipText, String fieldClass, String textClass) {
        TextField textField = new TextField();
        textField.setText(text);
        textField.setPromptText(tipText);
        if (StrUtil.isNotEmpty(fieldClass)) {
            textField.getStyleClass().add(fieldClass);
        }
        if (StrUtil.isNotEmpty(textClass)) {
            textField.getStyleClass().add(textClass);
        }
        return textField;
    }

    public static TextField createPasswordField(String text) {
        PasswordField textField = new PasswordField();
        textField.setText(text);
        return textField;
    }

    public static CheckBox createCheckBox(String text) {
        CheckBox checkBox = new CheckBox();
        checkBox.setText(text);
        return checkBox;
    }


    public static Button createBtn(String text, String btnClass, String textClass) {
        Button btn = new Button();
        btn.setText(text);
        btn.getStyleClass().add("btn");
        if (StrUtil.isNotBlank(btnClass)) {
            btn.getStyleClass().add(btnClass);
        }
        if (StrUtil.isNotBlank(textClass)) {
            btn.getStyleClass().add(textClass);
        }
        return btn;
    }

    public static Button createImgBtn(String url, int height, int width) {
        Button btn = new Button();
        if (width != 0) {
            btn.setPrefWidth(width);
        }
        if (height != 0) {
            btn.setPrefHeight(height);
        }
        String image = StrUtil.format("-fx-background-image: url('{}');", url);
        String stylePosition = "-fx-background-position: center;";
        String styleRepeat = "-fx-background-repeat: no-repeat;";
        btn.setStyle(image + styleRepeat + stylePosition);
        return btn;
    }

    public static Region createWrapperImg(String url, int height, int width, boolean fit) {
        Image image = new Image(url);
        ImageView imageView = new ImageView(image);
        HBox hBox = new HBox();
        hBox.getChildren().add(imageView);
        hBox.setPrefWidth(width);
        hBox.setPrefHeight(height);
        if (fit) {
            imageView.setFitWidth(width);
            imageView.setFitHeight(height);
        }
        hBox.setAlignment(Pos.CENTER);
        return hBox;
    }


    public static Region createWrapperImg(String url, int height, int width) {
        return createWrapperImg(url, height, width, true);
    }

    public static Hyperlink createHyperlink(String text) {
        Hyperlink link = new Hyperlink();
        link.setText(text);
        return link;
    }

    public static TableView createTableView() {
        return null;
    }


    public static Region createWindowBar(Region form, Stage stage) {
        HBox hBox = new HBox();
        hBox.setAlignment(Pos.CENTER_RIGHT);
        Region closeImg = createWrapperImg("img/close.png", 30, 40, false);
        hBox.getChildren().add(closeImg);
        FormBuilder girdBuilder = FormBuilder.queryForm(form);
        girdBuilder.setRow(0, GirdCell.createCell(1, Pos.CENTER))
                .coverCell(0, hBox)
                .endRow(false);


        closeImg.setOnMouseClicked(event -> {
            stage.close();
        });
        return hBox;
    }

    public static Label createPagination() {
        Label label = new Label();

        return label;
    }


    public static StackPane createStackPane() {

        return new StackPane();
    }


    public static String randomBgColor() {
        return randomBgColor(0.2);
    }

    public static String randomBgColor(double opacity) {
        int a = RandomUtil.randomInt(0, 255);
        int b = RandomUtil.randomInt(0, 255);
        int c = RandomUtil.randomInt(0, 255);
        String style = StrUtil.format("-fx-background-color: rgba({},{},{},{});", a, b, c, opacity);
        return style;
    }

    public static String randomHexBgColor() {
        int a = RandomUtil.randomInt(0, 255);
        int b = RandomUtil.randomInt(0, 255);
        int c = RandomUtil.randomInt(0, 255);
        String color = "#" + HexUtil.toHex(a) + HexUtil.toHex(b) + HexUtil.toHex(c);
        return color;
    }

    public static String setBgColor(String color) {
        String style = StrUtil.format("-fx-background-color: {};", color);
        return style;
    }

    public static String setBorder() {
        String bc = StrUtil.format(" -fx-border-color: #56c0e0;");
        String br = StrUtil.format(" -fx-border-radius: 3;");
        return bc + br;
    }


}
